package com.kehutong.work.order.manage.app;

import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.util.Objects;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.util.Token;
import com.kehutong.work.order.manage.entity.WorkOrderManageAppraisal;
import com.kehutong.work.order.manage.service.WorkOrderAppraisalService;

/**
 * 企微端-工单管理-评论相关
 */
@MvcAction
@ReqMapping("/kht-bin/work/order/manage/appraisal")
public class WorkOrderAppraisalAction {

    @Inject
    private WorkOrderAppraisalService appraisalService;

    @ReqMapping("/list/page")
    public Object listPage(JSONObject jsonObject) {
        return appraisalService.listPage(jsonObject);
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(Token token, WorkOrderManageAppraisal appraisal) {

        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        if (Objects.isNull(appraisal)) {
            return Result.error(0, "评论失败，不存在可评论的工单");
        }

        appraisal.setUserNo(token.getUuid());
        appraisal.setUserName(token.getUsername());

        return appraisalService.save(appraisal, token);
    }

    private boolean checkLogin(Token token){
        if (Objects.isNull(token)) {
            return false;
        }

        if (Objects.isEmpty(token.getUuid()) && Objects.isEmpty(token.getUsername())) {
            return false;
        } else {
            return true;
        }
    }

}
